<?php

namespace app\store\controller;

use app\store\model\Setting as SettingModel;
use think\Db;
use think\Request;
/**
 * 系统设置
 * Class Setting
 * @package app\store\controller
 */
class Setting extends Controller
{
    /**
     * 系统设置
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function store()
    {
        return $this->updateEvent('store');
    }
    /***
     * 云更新
     *
     *
     */
    public function Cloud()
    {
        $version = get_version();
        $request = Request::instance();
        $domain=$request->domain();
        $ulr = "https://api.qycompany.cn/index.php?s=/api/api/run/m/tpwe_pc/do/cloud/uniacid/1";
        $data = curlPost($ulr,array('version'=>$version['version'],'domain'=>$domain));
        $list = json_decode($data,true);
        $web_path = WEB_PATH;

        $lists =  array();
        if (!$this->request->isAjax()) {

            foreach ($list as $path=>$content){
                $new_md5_file = md5_file($web_path.$path);
                $filemtime = filemtime($web_path.$path);
                if($new_md5_file!=$content['filemd5']){
                    $r_dir= substr(base_convert(fileperms($web_path.$path), 10, 8), 3);
                    $lists[$path]=array(
                        'content'=>$content,
                        'filemtime'=>date("Y-m-d H:i:s",$filemtime),
                        'r_dir'=>$r_dir
                    );
                }
            }
            $version = get_version();
            return $this->fetch('cloud', ['list' => $lists,'version'=>$version]);
        }
        foreach ($list as $path=>$content){
            $new_md5_file = md5_file($web_path.$path);
            if($new_md5_file!=$content['filemd5']){
                //文件存在
                if(file_exists($web_path.$path)){
                    $r_dir= substr(base_convert(fileperms($web_path.$path), 10, 8), 3);
                    if($r_dir==777){
                        file_put_contents($web_path.$path, base64_decode($content['content']));
                    }

                }else{
                    $position = strrpos($web_path.$path,'/');
                    $paths = substr($web_path.$path,0,$position);
                    imkdirs($paths);
                    $fp=@fopen($web_path.$path,"a+");
                    @fputs($fp,base64_decode($content['content']));
                    @fclose($fp);
                   // @file_put_contents($web_path.$path, base64_decode($content['content']), LOCK_EX);

                }


            }


        }
        return $this->renderSuccess('升级成功');

    }
    /**
     * 交易设置
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function trade()
    {
        return $this->updateEvent('trade');
    }

    /**
     * 短信通知
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function sms()
    {
        return $this->updateEvent('sms');
    }

    /**
     * 模板消息
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function tplMsg()
    {
        return $this->updateEvent('tplMsg');
    }


    /**
     * 上传设置
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function storage()
    {
        return $this->updateEvent('storage');
    }


    /**
     * 更新系统设置事件
     * @param $key
     * @param $vars
     * @return array|mixed
     * @throws \think\exception\DbException
     */
    private function updateEvent($key, $vars = [])
    {
        if (!$this->request->isAjax()) {
            $vars['values'] = SettingModel::getItem($key);
            $vars['modules'] = Db::name('modules')->where(array('type'=>6))->select();
            return $this->fetch($key, $vars);
        }
        $model = new SettingModel;
        if ($model->edit($key, $this->postData($key))) {
            return $this->renderSuccess('操作成功');
        }
        return $this->renderError($model->getError() ?: '操作失败');
    }

}
